package cn.xc.org.controller;

import cn.xc.basic.util.AjaxResult;
import cn.xc.basic.util.PageList;
import cn.xc.org.domain.Employee;
import cn.xc.org.query.EmployeeQuery;
import cn.xc.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeSeriver;

    @GetMapping("/{id}")
    public Employee loadById(@PathVariable("id") Long id){
        return employeeSeriver.loadById(id);
    }

    @GetMapping
    public List<Employee> loadAll(){
        return employeeSeriver.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            employeeSeriver.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if (employee.getId()==null){
                employeeSeriver.save(employee);
            }else {
                employeeSeriver.update(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery){
            return employeeSeriver.queryPage(employeeQuery);
    }

    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids){
        try {
            employeeSeriver.patchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }



}
